package com.ysj.demo.accompanynetwork.mvp.model;

import android.util.Log;

import com.google.gson.Gson;
import com.ysj.demo.accompanynetwork.ApiServiceUntil;
import com.ysj.demo.accompanynetwork.bean.FavouritePost;
import com.ysj.demo.accompanynetwork.bean.PartnerPost;
import com.ysj.demo.accompanynetwork.bean.PartnerPostType;
import com.ysj.demo.accompanynetwork.bean.RestRsp;
import com.ysj.demo.accompanynetwork.mvp.common.OnCommonListener;
import com.ysj.demo.accompanynetwork.service.IPostQueryService;
import com.ysj.demo.accompanynetwork.service.bean.PostSearchModel;
import com.ysj.demo.accompanynetwork.service.bean.page.Page;
import com.ysj.demo.accompanynetwork.service.impl.PostQueryServiceImpl;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import java.util.List;

/**
 * 获取出租自己的列表
 */
public class GetRentSelfModel {

    private Gson gson = new Gson();

    public void search(PostSearchModel searchModel, final OnCommonListener onPostListener) {
        searchModel.setPostType(PartnerPostType.LENDSELF.getType());
        Call<RestRsp<Page<PartnerPost>>> call = ApiServiceUntil.getInstance().getPostService().searchPosts(searchModel);
        call.enqueue(new Callback<RestRsp<Page<PartnerPost>>>() {
            @Override
            public void onResponse( Call<RestRsp<Page<PartnerPost>>>call, Response<RestRsp<Page<PartnerPost>>> response) {
                RestRsp restRsp = response.body();
                onPostListener.onSuccessListener(restRsp);
            }

            @Override
            public void onFailure(Call<RestRsp<Page<PartnerPost>>> call, Throwable t) {
                onPostListener.onFailListener(t);
            }
        });

    }

    //将帖子加入收藏
    public void addFallow(String userid,String postid,final OnCommonListener onCommonListener){
        FavouritePost favouritePost = new FavouritePost();
        favouritePost.setId(userid);
        favouritePost.setPostid(postid);
        Call<RestRsp> call = ApiServiceUntil.getInstance().getPostService().watch(favouritePost);
        call.enqueue(new Callback<RestRsp>() {
            @Override
            public void onResponse(Call<RestRsp> call, Response<RestRsp> response) {
                onCommonListener.onSuccessListener(response.body());
            }

            @Override
            public void onFailure(Call<RestRsp> call, Throwable t) {
                onCommonListener.onFailListener(t);
            }
        });
    }


    //获取出租自己列表
    public void getSendSelfList(PostSearchModel postSearchModel,final OnCommonListener onListener) {
        Log.i("访问数据", new Gson().toJson(postSearchModel));
        try {
            Call<RestRsp<Page<PartnerPost>>> call = ApiServiceUntil.getInstance().getPostService().queryPostByUserId(postSearchModel);
            call.enqueue(new Callback<RestRsp<Page<PartnerPost>>>() {
                @Override
                public void onResponse(Call<RestRsp<Page<PartnerPost>>> call, Response<RestRsp<Page<PartnerPost>>> response) {
                   RestRsp<Page<PartnerPost>> pageRestRsp = response.body();
                    Log.i("列表=", gson.toJson(pageRestRsp));
                    Log.i("postservice ", "return posts size is {}" + (pageRestRsp.getData() != null ? pageRestRsp.getData().getRecords().size() : 0));
                    onListener.onSuccessListener(pageRestRsp);
                }

                @Override
                public void onFailure(Call<RestRsp<Page<PartnerPost>>> call, Throwable t) {
                    onListener.onFailListener(t);
                }
            });
        } catch (RuntimeException e) {
            onListener.onFailListener(e);
        }
    }



}
